package util;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Filter1 implements javax.servlet.Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("开始过滤");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //设置编码方式
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("utf-8");
        String requestURI = request.getRequestURI();
        if (requestURI.endsWith("success.jsp")&&request.getSession().getAttribute("a")==null){
            response.sendRedirect("/login.jsp");
        }
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("执行过滤器");
    }

    @Override
    public void destroy() {
        System.out.println("guolvqi销毁");
    }
}
